package com.suse.service.community.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.suse.service.community.service.FollowService;
import com.suse.service.community.util.RedisUtil;
import com.suse.service.community.util.UserHolder;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 关注业务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-09-02 18:17
 */
@Service
public class FollowServiceImpl implements FollowService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void follow(int targetId) {
        redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                Integer userId = UserHolder.getUser().getId();
                // 我关注的目标
                String attentionsKey = RedisUtil.getAttentionsKey(userId);
                // 目标的关注者
                String followersKey = RedisUtil.getFollowersKey(targetId);
                operations.multi();
                operations.opsForZSet().add(attentionsKey,targetId,System.currentTimeMillis());
                operations.opsForZSet().add(followersKey,userId,System.currentTimeMillis());
                return operations.exec();
            }
        });
    }

    @Override
    public void unfollow(int targetId) {
        redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                Integer userId = UserHolder.getUser().getId();
                // 我关注的目标
                String attentionsKey = RedisUtil.getAttentionsKey(userId);
                // 目标的关注者
                String followersKey = RedisUtil.getFollowersKey(targetId);
                operations.multi();
                operations.opsForZSet().remove(attentionsKey,targetId);
                operations.opsForZSet().remove(followersKey,userId);
                return operations.exec();
            }
        });
    }

    @Override
    public long getAttentionsCount(int userId) {
        String attentionsKey = RedisUtil.getAttentionsKey(userId);
        Long size = redisTemplate.opsForZSet().size(attentionsKey);
        if (ObjectUtil.isNull(size)) {
            return 0;
        }else {
            return size;
        }
    }

    @Override
    public long getFollowersCount(int userId) {
        String followersKey = RedisUtil.getFollowersKey(userId);
        Long size = redisTemplate.opsForZSet().size(followersKey);
        if (ObjectUtil.isNull(size)) {
            return 0;
        }else {
            return size;
        }
    }

    @Override
    public boolean hasFollowed(int targetId) {
        Integer userId = UserHolder.getUser().getId();
        String attentionsKey = RedisUtil.getAttentionsKey(userId);
        Double score = redisTemplate.opsForZSet().score(attentionsKey, targetId);
        return ObjectUtil.isNotNull(score);
    }

}
